<template>
	<div>
		<div class="h2">班级管理</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="名称">
							<a-input autocomplete="off" class="field-right" placeholder="请输入名称搜索"
								v-decorator="[`key`]" />
						</a-form-item>
					</a-col>
					<a-col :span="8" style="margin-top: -6px;">
						<a-button-group size="default" class="option">
							<a-button type="primary" @click="handleSearch">搜索</a-button>
							<a-button @click="handleReset">重置</a-button>
							<a-button type="primary" @click="addList">新增</a-button>
						</a-button-group>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div>
			<el-table ref="tableList" :data="data" v-loading="loading" border :max-height="tableHeight"
				highlight-current-row style="width: 100%;">
				<el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column>
				<el-table-column prop="Name" label="名称" min-width="120"></el-table-column>
				<el-table-column prop="DepartName" label="所属学院" min-width="100"></el-table-column>
				<el-table-column prop="DepartName" label="班主任" min-width="100">
					<template slot-scope="scope">
						<div v-if="scope.row.HeadMaster">{{ scope.row.HeadMaster }} / {{ scope.row.HeadMasterUserName }}
						</div>
						<div v-else>暂无</div>
					</template>
				</el-table-column>
				<el-table-column prop="ChargeUserNo" label="团支书" min-width="100">
					<template slot-scope="scope">
						<div v-if="scope.row.ChargeUserNo">{{ scope.row.ChargeUserNo }} / {{ scope.row.ChargeUserName }}
						</div>
						<div v-else>暂无</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120" fixed="right">
					<template slot-scope="scope">
						<a-tooltip placement="top">
							<template slot="title">修改</template>
							<i title="修改" class="iconfont icon-editor1" style="color: #1890FF;"
								@click="modifyList(scope.row, 'modify')"></i>
						</a-tooltip>
						<a-tooltip placement="top">
							<template slot="title">删除</template>
							<i title="删除" class="iconfont icon-delete" style="color: red;"
								@click="deleteList(scope.row, 1)"></i>
						</a-tooltip>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="pagination.currentPage" :page-sizes="pagination.pageSizeOptions"
				:page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="pagination.total"></el-pagination>
		</div>
		<add :text="text" v-show="addIf" :DepartCodeList="DepartCodeList" @closeFun="closeFun"></add>
	</div>
</template>

<script>
	import add from './add';
	import Func from '@/utils/func';
	import moment from 'moment';
	import Cache from '@/utils/cache';
	import {
		GetClassListManage,
		DeleteClass,
		GetDepartList
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			add
		},
		data() {
			return {
				data,
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				addIf: false,
				text: {},
				loading: false,
				values: {},
				DepartCodeList: [],
				tableHeight: parseFloat(window.innerHeight - 160),
				requestUrl: this.global.requestUrl,
				token: Cache.get('token'),
				isAssistantList: this.global.isAssistantList
			};
		},
		computed: {},
		created() {
			this.getList();
			this.getOtherList()
		},
		mounted() {},
		methods: {
			moment,
			async getOtherList() {
				let res = await GetDepartList()
				this.DepartCodeList = res.data.data
			},
			handleSearch(e) {
				e.preventDefault();
				this.form.validateFields((error, values) => {
					this.pagination.currentPage = 1;
					this.values = values;
					this.getList();
				});
			},
			handleReset() {
				this.form.resetFields();
				this.values = {};
				this.getList();
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList() {
				this.loading = true;
				let data = {};
				if (this.values) {
					data = this.values;
				}
				data.pageIndex = this.pagination.currentPage;
				data.pageSize = this.pagination.pageSize;
				let res = await GetClassListManage(data);
				this.loading = false;
				this.data = res.data.data;
				const pagination = {
					...this.pagination
				};
				pagination.total = res.data.pageInfo.TotalItemCount;
				this.pagination = pagination;
				this.$refs.tableList.bodyWrapper.scrollTop = 0;
			},
			addList() {
				this.text = {};
				this.addIf = !this.addIf;
			},
			modifyList(text) {
				text = {
					...text,
					time: new Date()
				}
				this.addIf = !this.addIf;
				this.text = text;
			},
			deleteList(text, type) {
				let that = this;
				let content = '';
				switch (type) {
					case 1:
						content = '您确定删除该班级吗?';
						break;
					default:
						break;
				}
				this.$confirm({
					title: '提示',
					content: content,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = await DeleteClass(data);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			closeFun(data) {
				this.addIf = false;
				if (data == 1) {
					this.getList();
				}
			}
		}
	};
</script>

<style lang="less" scoped>
	.home-form /deep/.ant-form-item {
		display: flex;
		margin-bottom: 10px !important;
	}

	.home-form /deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	.home-form /deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	.home-form /deep/.ant-calendar-picker {
		width: 100%;
	}

	/deep/.el-table th.is-leaf {
		background: #fafafa;
	}

	/deep/.el-table td,
	/deep/.el-table th {
		padding: 5px;
	}

	.option {
		margin-top: 10px;
		margin-bottom: 20px;
		overflow: hidden;

		.ant-btn {
			font-size: 12px;
		}
	}

	.el-pagination {
		margin-top: 10px;
	}

	/deep/ .ant-form-item-control-wrapper {
		min-width: 200px;
	}
</style>
